import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../utils/g.dart';

// import '../../providers/UserProvider.dart';
// import 'package:provider/provider.dart';

class Login extends StatefulWidget {
  Login({Key? key}) : super(key: key);

  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  String _phone = '';
  String _password = '';
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: null,
      body: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        padding: EdgeInsets.all(20),
        physics: BouncingScrollPhysics(),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: 300,
              child: Column(
                children: [
                  SizedBox(height: 30),
                  Center(
                    child: Image.asset('assets/images/flutter.jpg', width: 200),
                  ),
                  SizedBox(height: 50),
                  Form(
                    key: _formKey,
                    child: Column(
                      children: [
                        TextFormField(
                          keyboardType: TextInputType.number,
                          decoration: InputDecoration(
                            prefixIcon: Icon(Icons.phone),
                            fillColor: Colors.deepPurple[100],
                            filled: true,
                            border: InputBorder.none,
                            hintText: '手机号'
                          ),
                          validator: (value) {
                            RegExp reg = new RegExp(r'^\d{11}$');
                            if (!reg.hasMatch(value!)) {
                              return '手机号非法';
                            }
                            return null;
                          },
                          onSaved: (value) {
                            print('_phone onSaved');
                            _phone = value as String;
                          },
                        ),
                        SizedBox(height: 10),
                        TextFormField(
                          obscureText: true,
                          decoration: InputDecoration(
                            prefixIcon: Icon(Icons.password),
                            fillColor: Colors.deepPurple[100],
                            filled: true,
                            border: InputBorder.none,
                            hintText: '密码'
                          ),
                          validator: (value) {
                            return value?.length as num < 6 ? "密码长度不够": null;
                          },
                          onSaved: (value) {
                            print('_password onSaved');
                            _password = value as String;
                          },
                        )
                      ],
                    )
                  ),
                  SizedBox(height: 10),
                  Row(
                    children: [
                      Expanded(
                        child: ElevatedButton(
                          onPressed: () async {
                            if (_formKey.currentState!.validate()) {
                              // // print('_formKey.currentState.save() - Before');
                              // _formKey.currentState?.save();
                              // // print('_formKey.currentState.save() - After');
                              
                              // // 调用登录接口
                              // var res = await G.api.user.login(phone: _phone, password: _password);
                              // if (res != false && res['status'] == 200) {
                              //   _saveUserToken(res['data']);

                              //   // G.router.pop(context);
                              //   Future.delayed(Duration(milliseconds: 500), () { 
                              //     G.router.navigateTo(context, '/mine');
                              //   });
                              // } else {
                              //   G.error('登录失败');
                              //   _formKey.currentState?.reset();
                              // }
                            }
                          }, 
                          child: Text('登陆'),
                          style: ButtonStyle(
                            padding: MaterialStateProperty.all(EdgeInsets.all(20)),
                            // shape: MaterialStateProperty.all(StadiumBorder()),
                          )
                        ),
                      ),
                    ]
                  ),
                  SizedBox(height: 10),

                  G.footer(),
                ],
              ),
            )
      
          ],
        ),
        
      ),
    );
  }

  _saveUserToken(Map res) async {
    // 获取保存实例
    SharedPreferences prefs = G.prefs();
    // String token = prefs.getString('toekn') ?? '';
    // print(res);
    await prefs.setString('token', res['token']);
    await prefs.setInt('expires_time', res['expires_time']);
  }

  @override
  void dispose() {
    super.dispose();
  }
}